// datetimebutton.cpp
#include "DateTimeButton.h"
#include <QDateTimeEdit>
#include <QMouseEvent>
#include <QStylePainter>
#include <QStyleOptionSpinBox>
#include <QApplication>

DateTimeButton::DateTimeButton(QWidget *parent)
    : QDateTimeEdit(parent), m_internalUpdate(false)
{

    setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    // 设置默认时间为当前时间
    // setDateTime(QDateTime::currentDateTime());
    // 设置为只读模式
    setReadOnly(true);
    // 连接内部信号
    connect(this, &DateTimeButton::dateTimeChanged,
            [this]()
            {
                if (!m_internalUpdate)
                {
                    // 如果不是内部更新，则重新绘制
                    update();
                }
            });
}

QSize DateTimeButton::sizeHint() const
{
    // 返回适合按钮样式的大小
    QFontMetrics fm(font());
    int textWidth = fm.horizontalAdvance(dateTime().toString(displayFormat()));
    int iconWidth = style()->pixelMetric(QStyle::PM_ButtonIconSize, nullptr, this);
    int width = textWidth + iconWidth + 20;
    int height = qMax(fm.height(), 14) + 10;
    return QSize(width, height);
}

QSize DateTimeButton::minimumSizeHint() const
{
    return sizeHint();
}

void DateTimeButton::mousePressEvent(QMouseEvent *event)
{
    // 忽略常规的 QDateTimeEdit 处理，直接显示自定义对话框
    if (event->button() == Qt::LeftButton)
    {
        showDateTimeDialog();
        event->accept();
    }
    else
    {
        QDateTimeEdit::mousePressEvent(event);
    }
}

void DateTimeButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QStylePainter painter(this);
    QStyleOptionSpinBox option;
    initStyleOption(&option);

    // 绘制框架
    painter.drawComplexControl(QStyle::CC_SpinBox, option);

    // 绘制文本
    QRect textRect = style()->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxEditField, this);
    textRect.adjust(2, 0, -2, 0); // 边距调整

    QString text = dateTime().toString(displayFormat());
    QFontMetrics fm = painter.fontMetrics();

    // 文本对齐
    Qt::Alignment align = alignment();
    if (align.testFlag(Qt::AlignHCenter))
        align = Qt::AlignCenter;
    else if (align.testFlag(Qt::AlignRight))
        align = Qt::AlignRight | Qt::AlignVCenter;
    else
        align = Qt::AlignLeft | Qt::AlignVCenter;

    painter.drawText(textRect, align, text);
}

void DateTimeButton::showDateTimeDialog()
{
    createDateTimeDialog();
}
void DateTimeButton::createDateTimeDialog()
{
    QDialog dialog(this);
    dialog.setWindowTitle(tr("选择日期时间"));
    dialog.setWindowModality(Qt::WindowModal);

    QVBoxLayout *layout = new QVBoxLayout(&dialog);
    layout->setSpacing(15); // 增加布局间距

    // 日期选择区域 - 使用分组框增强辨识度
    QGroupBox *dateGroup = new QGroupBox(tr("日期设置"), &dialog);
    QHBoxLayout *dateLayout = new QHBoxLayout(dateGroup);
    dateLayout->setSpacing(20); // 增加控件间距离

    // 年份选择
    QFrame *yearFrame = new QFrame(&dialog);
    yearFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    yearFrame->setLineWidth(2);
    QVBoxLayout *yearLayout = new QVBoxLayout(yearFrame);
    yearLayout->setSpacing(5);
    QLabel *yearTitle = new QLabel(tr("年"), &dialog);
    yearTitle->setAlignment(Qt::AlignCenter);
    QFont yearFont = yearTitle->font();
    yearFont.setBold(true);
    yearTitle->setFont(yearFont);

    QPushButton *yearUpBtn = new QPushButton("▲", &dialog);
    yearUpBtn->setFixedSize(40, 30);
    QFont arrowFont = yearUpBtn->font();
    // arrowFont.setPointSize(12);
    arrowFont.setBold(true);
    yearUpBtn->setFont(arrowFont);

    QLabel *yearLabel = new QLabel(&dialog);
    yearLabel->setAlignment(Qt::AlignCenter);
    yearLabel->setFixedSize(80, 30);
    QFont valueFont = yearLabel->font();
    // valueFont.setPointSize(14);
    valueFont.setBold(true);
    yearLabel->setFont(valueFont);
    yearLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *yearDownBtn = new QPushButton("▼", &dialog);
    yearDownBtn->setFixedSize(40, 30);
    yearDownBtn->setFont(arrowFont);

    yearLayout->addWidget(yearTitle);
    yearLayout->addWidget(yearUpBtn, 0, Qt::AlignCenter);
    yearLayout->addWidget(yearLabel, 0, Qt::AlignCenter);
    yearLayout->addWidget(yearDownBtn, 0, Qt::AlignCenter);

    // 月份选择
    QFrame *monthFrame = new QFrame(&dialog);
    monthFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    monthFrame->setLineWidth(2);
    QVBoxLayout *monthLayout = new QVBoxLayout(monthFrame);
    monthLayout->setSpacing(5);
    QLabel *monthTitle = new QLabel(tr("月"), &dialog);
    monthTitle->setAlignment(Qt::AlignCenter);
    monthTitle->setFont(yearFont);

    QPushButton *monthUpBtn = new QPushButton("▲", &dialog);
    monthUpBtn->setFixedSize(40, 30);
    monthUpBtn->setFont(arrowFont);

    QLabel *monthLabel = new QLabel(&dialog);
    monthLabel->setAlignment(Qt::AlignCenter);
    monthLabel->setFixedSize(60, 30);
    monthLabel->setFont(valueFont);
    monthLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *monthDownBtn = new QPushButton("▼", &dialog);
    monthDownBtn->setFixedSize(40, 30);
    monthDownBtn->setFont(arrowFont);

    monthLayout->addWidget(monthTitle);
    monthLayout->addWidget(monthUpBtn, 0, Qt::AlignCenter);
    monthLayout->addWidget(monthLabel, 0, Qt::AlignCenter);
    monthLayout->addWidget(monthDownBtn, 0, Qt::AlignCenter);

    // 日期选择
    QFrame *dayFrame = new QFrame(&dialog);
    dayFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    dayFrame->setLineWidth(2);
    QVBoxLayout *dayLayout = new QVBoxLayout(dayFrame);
    dayLayout->setSpacing(5);
    QLabel *dayTitle = new QLabel(tr("日"), &dialog);
    dayTitle->setAlignment(Qt::AlignCenter);
    dayTitle->setFont(yearFont);

    QPushButton *dayUpBtn = new QPushButton("▲", &dialog);
    dayUpBtn->setFixedSize(40, 30);
    dayUpBtn->setFont(arrowFont);

    QLabel *dayLabel = new QLabel(&dialog);
    dayLabel->setAlignment(Qt::AlignCenter);
    dayLabel->setFixedSize(60, 30);
    dayLabel->setFont(valueFont);
    dayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *dayDownBtn = new QPushButton("▼", &dialog);
    dayDownBtn->setFixedSize(40, 30);
    dayDownBtn->setFont(arrowFont);

    dayLayout->addWidget(dayTitle);
    dayLayout->addWidget(dayUpBtn, 0, Qt::AlignCenter);
    dayLayout->addWidget(dayLabel, 0, Qt::AlignCenter);
    dayLayout->addWidget(dayDownBtn, 0, Qt::AlignCenter);

    dateLayout->addWidget(yearFrame);
    dateLayout->addWidget(monthFrame);
    dateLayout->addWidget(dayFrame);

    // 时间选择区域 - 使用分组框增强辨识度
    QGroupBox *timeGroup = new QGroupBox(tr("时间设置"), &dialog);
    QHBoxLayout *timeLayout = new QHBoxLayout(timeGroup);
    timeLayout->setSpacing(20); // 增加控件间距离

    // 小时选择
    QFrame *hourFrame = new QFrame(&dialog);
    hourFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    hourFrame->setLineWidth(2);
    QVBoxLayout *hourLayout = new QVBoxLayout(hourFrame);
    hourLayout->setSpacing(5);
    QLabel *hourTitle = new QLabel(tr("时"), &dialog);
    hourTitle->setAlignment(Qt::AlignCenter);
    hourTitle->setFont(yearFont);

    QPushButton *hourUpBtn = new QPushButton("▲", &dialog);
    hourUpBtn->setFixedSize(40, 30);
    hourUpBtn->setFont(arrowFont);

    QLabel *hourLabel = new QLabel(&dialog);
    hourLabel->setAlignment(Qt::AlignCenter);
    hourLabel->setFixedSize(60, 30);
    hourLabel->setFont(valueFont);
    hourLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *hourDownBtn = new QPushButton("▼", &dialog);
    hourDownBtn->setFixedSize(40, 30);
    hourDownBtn->setFont(arrowFont);

    hourLayout->addWidget(hourTitle);
    hourLayout->addWidget(hourUpBtn, 0, Qt::AlignCenter);
    hourLayout->addWidget(hourLabel, 0, Qt::AlignCenter);
    hourLayout->addWidget(hourDownBtn, 0, Qt::AlignCenter);

    // 分钟选择
    QFrame *minuteFrame = new QFrame(&dialog);
    minuteFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    minuteFrame->setLineWidth(2);
    QVBoxLayout *minuteLayout = new QVBoxLayout(minuteFrame);
    minuteLayout->setSpacing(5);
    QLabel *minuteTitle = new QLabel(tr("分"), &dialog);
    minuteTitle->setAlignment(Qt::AlignCenter);
    minuteTitle->setFont(yearFont);

    QPushButton *minuteUpBtn = new QPushButton("▲", &dialog);
    minuteUpBtn->setFixedSize(40, 30);
    minuteUpBtn->setFont(arrowFont);

    QLabel *minuteLabel = new QLabel(&dialog);
    minuteLabel->setAlignment(Qt::AlignCenter);
    minuteLabel->setFixedSize(60, 30);
    minuteLabel->setFont(valueFont);
    minuteLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *minuteDownBtn = new QPushButton("▼", &dialog);
    minuteDownBtn->setFixedSize(40, 30);
    minuteDownBtn->setFont(arrowFont);

    minuteLayout->addWidget(minuteTitle);
    minuteLayout->addWidget(minuteUpBtn, 0, Qt::AlignCenter);
    minuteLayout->addWidget(minuteLabel, 0, Qt::AlignCenter);
    minuteLayout->addWidget(minuteDownBtn, 0, Qt::AlignCenter);

    // 秒选择
    QFrame *secondFrame = new QFrame(&dialog);
    secondFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    secondFrame->setLineWidth(2);
    QVBoxLayout *secondLayout = new QVBoxLayout(secondFrame);
    secondLayout->setSpacing(5);
    QLabel *secondTitle = new QLabel(tr("秒"), &dialog);
    secondTitle->setAlignment(Qt::AlignCenter);
    secondTitle->setFont(yearFont);

    QPushButton *secondUpBtn = new QPushButton("▲", &dialog);
    secondUpBtn->setFixedSize(40, 30);
    secondUpBtn->setFont(arrowFont);

    QLabel *secondLabel = new QLabel(&dialog);
    secondLabel->setAlignment(Qt::AlignCenter);
    secondLabel->setFixedSize(60, 30);
    secondLabel->setFont(valueFont);
    secondLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QPushButton *secondDownBtn = new QPushButton("▼", &dialog);
    secondDownBtn->setFixedSize(40, 30);
    secondDownBtn->setFont(arrowFont);

    secondLayout->addWidget(secondTitle);
    secondLayout->addWidget(secondUpBtn, 0, Qt::AlignCenter);
    secondLayout->addWidget(secondLabel, 0, Qt::AlignCenter);
    secondLayout->addWidget(secondDownBtn, 0, Qt::AlignCenter);

    timeLayout->addWidget(hourFrame);
    timeLayout->addWidget(minuteFrame);
    timeLayout->addWidget(secondFrame);

    layout->addWidget(dateGroup);
    layout->addWidget(timeGroup);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *okButton = new QPushButton(tr("确定"), &dialog);
    QPushButton *cancelButton = new QPushButton(tr("取消"), &dialog);

    // 设置按钮较大尺寸和粗体文字
    QFont buttonFont = okButton->font();
    buttonFont.setBold(true);
    // buttonFont.setPointSize(11);
    okButton->setFont(buttonFont);
    okButton->setMinimumSize(80, 35);
    cancelButton->setFont(buttonFont);
    cancelButton->setMinimumSize(80, 35);

    buttonLayout->addStretch();
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addStretch();

    layout->addLayout(buttonLayout);

    // 初始化显示值为当前日期时间（格式化为两位数）
    QDateTime currentDateTime = dateTime();
    yearLabel->setText(QString::number(currentDateTime.date().year()));
    monthLabel->setText(QString("%1").arg(currentDateTime.date().month(), 2, 10, QChar('0')));
    dayLabel->setText(QString("%1").arg(currentDateTime.date().day(), 2, 10, QChar('0')));
    hourLabel->setText(QString("%1").arg(currentDateTime.time().hour(), 2, 10, QChar('0')));
    minuteLabel->setText(QString("%1").arg(currentDateTime.time().minute(), 2, 10, QChar('0')));
    secondLabel->setText(QString("%1").arg(currentDateTime.time().second(), 2, 10, QChar('0')));

    // 连接按钮信号
    connect(yearUpBtn, &QPushButton::clicked, [=]()
            {
        int year = yearLabel->text().toInt() + 1;
        yearLabel->setText(QString::number(year)); });

    connect(yearDownBtn, &QPushButton::clicked, [=]()
            {
        int year = yearLabel->text().toInt() - 1;
        yearLabel->setText(QString::number(year)); });

    connect(monthUpBtn, &QPushButton::clicked, [=]()
            {
        int month = monthLabel->text().toInt() + 1;
        if (month > 12) month = 1;
        monthLabel->setText(QString("%1").arg(month, 2, 10, QChar('0'))); });

    connect(monthDownBtn, &QPushButton::clicked, [=]()
            {
        int month = monthLabel->text().toInt() - 1;
        if (month < 1) month = 12;
        monthLabel->setText(QString("%1").arg(month, 2, 10, QChar('0'))); });

    // 添加日按钮的点击事件处理
    connect(dayUpBtn, &QPushButton::clicked, [=]()
            {
        QDate date(yearLabel->text().toInt(), monthLabel->text().toInt(), 1);
        int daysInMonth = date.daysInMonth();
        int day = dayLabel->text().toInt() + 1;
        if (day > daysInMonth) day = 1;
        dayLabel->setText(QString("%1").arg(day, 2, 10, QChar('0'))); });

    connect(dayDownBtn, &QPushButton::clicked, [=]()
            {
        QDate date(yearLabel->text().toInt(), monthLabel->text().toInt(), 1);
        int daysInMonth = date.daysInMonth();
        int day = dayLabel->text().toInt() - 1;
        if (day < 1) day = daysInMonth;
        dayLabel->setText(QString("%1").arg(day, 2, 10, QChar('0'))); });

    // 添加小时按钮的点击事件处理
    connect(hourUpBtn, &QPushButton::clicked, [=]()
            {
        int hour = hourLabel->text().toInt() + 1;
        if (hour > 23) hour = 0;
        hourLabel->setText(QString("%1").arg(hour, 2, 10, QChar('0'))); });

    connect(hourDownBtn, &QPushButton::clicked, [=]()
            {
        int hour = hourLabel->text().toInt() - 1;
        if (hour < 0) hour = 23;
        hourLabel->setText(QString("%1").arg(hour, 2, 10, QChar('0'))); });

    // 添加分钟按钮的点击事件处理
    connect(minuteUpBtn, &QPushButton::clicked, [=]()
            {
        int minute = minuteLabel->text().toInt() + 1;
        if (minute > 59) minute = 0;
        minuteLabel->setText(QString("%1").arg(minute, 2, 10, QChar('0'))); });

    connect(minuteDownBtn, &QPushButton::clicked, [=]()
            {
        int minute = minuteLabel->text().toInt() - 1;
        if (minute < 0) minute = 59;
        minuteLabel->setText(QString("%1").arg(minute, 2, 10, QChar('0'))); });

    // 添加秒钟按钮的点击事件处理
    connect(secondUpBtn, &QPushButton::clicked, [=]()
            {
        int second = secondLabel->text().toInt() + 1;
        if (second > 59) second = 0;
        secondLabel->setText(QString("%1").arg(second, 2, 10, QChar('0'))); });

    connect(secondDownBtn, &QPushButton::clicked, [=]()
            {
        int second = secondLabel->text().toInt() - 1;
        if (second < 0) second = 59;
        secondLabel->setText(QString("%1").arg(second, 2, 10, QChar('0'))); });

    connect(okButton, &QPushButton::clicked, [&]()
            {
        int year = yearLabel->text().toInt();
        int month = monthLabel->text().toInt();
        int day = dayLabel->text().toInt();
        int hour = hourLabel->text().toInt();
        int minute = minuteLabel->text().toInt();
        int second = secondLabel->text().toInt();

        QDateTime selectedDateTime(QDate(year, month, day), QTime(hour, minute, second));
        m_internalUpdate = true;
        setDateTime(selectedDateTime);
        m_internalUpdate = false;
        dialog.accept(); });

    connect(cancelButton, &QPushButton::clicked, &dialog, &QDialog::reject);

    // 显示对话框
    dialog.exec();
}
// void DateTimeButton::createDateTimeDialog()
// {
//     QDialog dialog(this);
//     dialog.setWindowTitle(tr("选择日期时间"));
//     dialog.setWindowModality(Qt::WindowModal);

//     QVBoxLayout *layout = new QVBoxLayout(&dialog);

//     // 创建各个组件
//     QHBoxLayout *dateLayout = new QHBoxLayout();

//     // 年份选择
//     QVBoxLayout *yearLayout = new QVBoxLayout();
//     QLabel *yearLabel = new QLabel(tr("年"), &dialog);
//     QPushButton *yearUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *yearDownBtn = new QPushButton("▼", &dialog);
//     yearLayout->addWidget(yearUpBtn);
//     yearLayout->addWidget(yearLabel);
//     yearLayout->addWidget(yearDownBtn);

//     // 月份选择
//     QVBoxLayout *monthLayout = new QVBoxLayout();
//     QLabel *monthLabel = new QLabel(tr("月"), &dialog);
//     QPushButton *monthUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *monthDownBtn = new QPushButton("▼", &dialog);
//     monthLayout->addWidget(monthUpBtn);
//     monthLayout->addWidget(monthLabel);
//     monthLayout->addWidget(monthDownBtn);

//     // 日期选择
//     QVBoxLayout *dayLayout = new QVBoxLayout();
//     QLabel *dayLabel = new QLabel(tr("日"), &dialog);
//     QPushButton *dayUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *dayDownBtn = new QPushButton("▼", &dialog);
//     dayLayout->addWidget(dayUpBtn);
//     dayLayout->addWidget(dayLabel);
//     dayLayout->addWidget(dayDownBtn);

//     dateLayout->addLayout(yearLayout);
//     dateLayout->addLayout(monthLayout);
//     dateLayout->addLayout(dayLayout);

//     // 时间选择
//     QHBoxLayout *timeLayout = new QHBoxLayout();

//     // 小时选择
//     QVBoxLayout *hourLayout = new QVBoxLayout();
//     QLabel *hourLabel = new QLabel(tr("时"), &dialog);
//     QPushButton *hourUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *hourDownBtn = new QPushButton("▼", &dialog);
//     hourLayout->addWidget(hourUpBtn);
//     hourLayout->addWidget(hourLabel);
//     hourLayout->addWidget(hourDownBtn);

//     // 分钟选择
//     QVBoxLayout *minuteLayout = new QVBoxLayout();
//     QLabel *minuteLabel = new QLabel(tr("分"), &dialog);
//     QPushButton *minuteUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *minuteDownBtn = new QPushButton("▼", &dialog);
//     minuteLayout->addWidget(minuteUpBtn);
//     minuteLayout->addWidget(minuteLabel);
//     minuteLayout->addWidget(minuteDownBtn);

//     // 秒选择
//     QVBoxLayout *secondLayout = new QVBoxLayout();
//     QLabel *secondLabel = new QLabel(tr("秒"), &dialog);
//     QPushButton *secondUpBtn = new QPushButton("▲", &dialog);
//     QPushButton *secondDownBtn = new QPushButton("▼", &dialog);
//     secondLayout->addWidget(secondUpBtn);
//     secondLayout->addWidget(secondLabel);
//     secondLayout->addWidget(secondDownBtn);

//     timeLayout->addLayout(hourLayout);
//     timeLayout->addLayout(minuteLayout);
//     timeLayout->addLayout(secondLayout);

//     layout->addLayout(dateLayout);
//     layout->addLayout(timeLayout);

//     // 创建按钮布局
//     QHBoxLayout *buttonLayout = new QHBoxLayout();
//     QPushButton *okButton = new QPushButton(tr("确定"), &dialog);
//     QPushButton *cancelButton = new QPushButton(tr("取消"), &dialog);

//     buttonLayout->addWidget(okButton);
//     buttonLayout->addWidget(cancelButton);

//     layout->addLayout(buttonLayout);

//     // 初始化显示值为当前日期时间
//     QDateTime currentDateTime = dateTime();
//     yearLabel->setText(QString::number(currentDateTime.date().year()));
//     monthLabel->setText(QString::number(currentDateTime.date().month()));
//     dayLabel->setText(QString::number(currentDateTime.date().day()));
//     hourLabel->setText(QString::number(currentDateTime.time().hour()));
//     minuteLabel->setText(QString::number(currentDateTime.time().minute()));
//     secondLabel->setText(QString::number(currentDateTime.time().second()));

//     // 连接按钮信号
//     connect(yearUpBtn, &QPushButton::clicked, [=]()
//             {
//         int year = yearLabel->text().toInt() + 1;
//         yearLabel->setText(QString::number(year)); });

//     connect(yearDownBtn, &QPushButton::clicked, [=]()
//             {
//         int year = yearLabel->text().toInt() - 1;
//         yearLabel->setText(QString::number(year)); });

//     connect(monthUpBtn, &QPushButton::clicked, [=]()
//             {
//         int month = monthLabel->text().toInt() + 1;
//         if (month > 12) month = 1;
//         monthLabel->setText(QString::number(month)); });

//     connect(monthDownBtn, &QPushButton::clicked, [=]()
//             {
//         int month = monthLabel->text().toInt() - 1;
//         if (month < 1) month = 12;
//         monthLabel->setText(QString::number(month)); });

//     // 添加日按钮的点击事件处理
//     connect(dayUpBtn, &QPushButton::clicked, [=]()
//             {
//         QDate date(yearLabel->text().toInt(), monthLabel->text().toInt(), 1);
//         int daysInMonth = date.daysInMonth();
//         int day = dayLabel->text().toInt() + 1;
//         if (day > daysInMonth) day = 1;
//         dayLabel->setText(QString::number(day)); });

//     connect(dayDownBtn, &QPushButton::clicked, [=]()
//             {
//         QDate date(yearLabel->text().toInt(), monthLabel->text().toInt(), 1);
//         int daysInMonth = date.daysInMonth();
//         int day = dayLabel->text().toInt() - 1;
//         if (day < 1) day = daysInMonth;
//         dayLabel->setText(QString::number(day)); });

//     // 添加小时按钮的点击事件处理
//     connect(hourUpBtn, &QPushButton::clicked, [=]()
//             {
//         int hour = hourLabel->text().toInt() + 1;
//         if (hour > 23) hour = 0;
//         hourLabel->setText(QString::number(hour)); });

//     connect(hourDownBtn, &QPushButton::clicked, [=]()
//             {
//         int hour = hourLabel->text().toInt() - 1;
//         if (hour < 0) hour = 23;
//         hourLabel->setText(QString::number(hour)); });

//     // 添加分钟按钮的点击事件处理
//     connect(minuteUpBtn, &QPushButton::clicked, [=]()
//             {
//         int minute = minuteLabel->text().toInt() + 1;
//         if (minute > 59) minute = 0;
//         minuteLabel->setText(QString::number(minute)); });

//     connect(minuteDownBtn, &QPushButton::clicked, [=]()
//             {
//         int minute = minuteLabel->text().toInt() - 1;
//         if (minute < 0) minute = 59;
//         minuteLabel->setText(QString::number(minute)); });

//     // 添加秒钟按钮的点击事件处理
//     connect(secondUpBtn, &QPushButton::clicked, [=]()
//             {
//         int second = secondLabel->text().toInt() + 1;
//         if (second > 59) second = 0;
//         secondLabel->setText(QString::number(second)); });

//     connect(secondDownBtn, &QPushButton::clicked, [=]()
//             {
//         int second = secondLabel->text().toInt() - 1;
//         if (second < 0) second = 59;
//         secondLabel->setText(QString::number(second)); });

//     connect(okButton, &QPushButton::clicked, [&]()
//             {
//         int year = yearLabel->text().toInt();
//         int month = monthLabel->text().toInt();
//         int day = dayLabel->text().toInt();
//         int hour = hourLabel->text().toInt();
//         int minute = minuteLabel->text().toInt();
//         int second = secondLabel->text().toInt();

//         QDateTime selectedDateTime(QDate(year, month, day), QTime(hour, minute, second));
//         m_internalUpdate = true;
//         setDateTime(selectedDateTime);
//         m_internalUpdate = false;
//         dialog.accept(); });

//     connect(cancelButton, &QPushButton::clicked, &dialog, &QDialog::reject);

//     // 显示对话框
//     dialog.exec();
// }